import { ClassicModel } from '../../models/classic.js'
let classic = new ClassicModel()
//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    classicData: null,
    latest: true,
    first: false,
    likeCount: 0,
    likeStatus: false,
  },
  //事件处理函数
  onLoad: function (options) {
    classic.getLatest((res) => {
      console.log(res)
      this.setData({
        classicData: res.data,
        likeStatus: res.data.like_status,
        likeCount: res.data.fav_nums
      })
    })
  },
  onLike: function (event) {
    let behavior = event.detail.behavior
    classic.onLike(behavior, this.data.classicData.id, this.data.classicData.type)
  },

  onPrevious: function () {
    this._updateClassic('previous')
  },
  onNext: function () {
    this._updateClassic('next')
  },
  _updateClassic: function (nextOrPrevious) {
    let index = this.data.classicData.index
    classic.getClassic(index, nextOrPrevious, (res) => {
      this._getLikeStatus(res.data.id, res.data.type)
      this.setData({
        classicData: res.data,
        latest: classic.isLatest(res.data.index),
        first: classic.isFirst(res.data.index),
      })
    })
  },
  _getLikeStatus(artID, category) {
    classic.getClassicLikeStatus(artID, category,
      (res) => {
        this.setData({
          likeCount: res.data.fav_nums,
          likeStatus: res.data.like_status
        })
      }
    )
  }
})
